package com.david.avcodec.system

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Bundle
import com.david.avcodec.R
import android.view.View
import android.media.AudioRecord
import android.media.AudioFormat
import android.media.MediaRecorder
import android.widget.Toast
import androidx.core.app.ActivityCompat

//audioRecord／mediaRecord录制音频、audioTrack播放音频
//参考链接：https://developer.aliyun.com/article/1234244?spm=a2c6h.14164896.0.0.26024078jSZFR4   https://blog.csdn.net/yu540135101/article/details/105589424   https://blog.csdn.net/u010029439/article/details/85056767

class MyAudioRecordActivity: Activity() {
	private var mRecord: AudioRecord? = null
	private var minBufferSize = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_audio)

        val sampleRate = 44100
        minBufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO,
		AudioFormat.ENCODING_PCM_16BIT)


        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.RECORD_AUDIO
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            Toast.makeText(this, "请先添加录音权限", Toast.LENGTH_LONG).show()
            return
        }
        mRecord = AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO,
		AudioFormat.ENCODING_PCM_16BIT, minBufferSize)


        findViewById<View>(R.id.start).setOnClickListener {
            try {
				mRecord?.startRecording()
			} catch (e: Exception) {
				e.printStackTrace()
			}
        }

        findViewById<View>(R.id.stop).setOnClickListener {
            
        }
    }
}